﻿//
// GNS_PromoterPro.cpp
// MobileManageSys
//FuncTion 推广员充值比例
// Created by mr.l on 16/1/14.
//
//
#include "GnsClientFrame.h"
#include "GNS_PromoterPro.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "GNS_PlayerMgr.h"
#include "GNS_Common.h"
#include "GNS_SceneMgr.h"
#include "GnsOften.h"

USING_NS_CC;
using namespace cocostudio::timeline;
using namespace ui;

////extern ISocketMgr * _GameSock;
#define winsize Director::getInstance()->getVisibleSize()

GNS_PromoterPro::GNS_PromoterPro()
{
    pGNS_PromoterPro=0;
    //InputNum=nullptr;
    pInputNun=nullptr;
    SaveInput=0;
}

GNS_PromoterPro::~GNS_PromoterPro()
{
    
}

GNS_PromoterPro* GNS_PromoterPro::createLayer()
{
    auto pGNS_PromoterPro = GNS_PromoterPro::create();
    auto pScene = Director::getInstance()->getRunningScene();
    if (!pScene)
        return 0;
    pScene->addChild(pGNS_PromoterPro);
    return pGNS_PromoterPro;
}

bool GNS_PromoterPro::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    return true;
}
void GNS_PromoterPro::SetNodeName(std::string CsbName)
{
    pGNS_PromoterPro = CSLoader::createNode(CsbName);
    pGNS_PromoterPro->setContentSize(winsize);
    ui::Helper::doLayout(pGNS_PromoterPro);
    addChild(pGNS_PromoterPro,11);
    GNS_PlayerMgr::getInstance()->_NodeChildVec.pushBack(pGNS_PromoterPro);
    auto pBackBtn = static_cast<Button*>(pGNS_PromoterPro->getChildByName("PromoterTopBackBtn"));
    pBackBtn->setTag(10);
    pBackBtn->addTouchEventListener(CC_CALLBACK_2(GNS_PromoterPro::BtnCallBack, this));
    
	tfSetProRatio  = static_cast<TextField*>(pGNS_PromoterPro->getChildByName("TextField_1"));
	tfSetProRatio->setVisible(true);
	tfSetProRatio->setString(StringUtils::format("%d", SaveInput));
	tfSetProRatio->setVisible(false);

	_tfSetProRatio = GNS_Handler::createTMEditBoxWithExistFeild(tfSetProRatio, pGNS_PromoterPro, 10, EditBox::InputMode::NUMERIC, true);
	_tfSetProRatio->setText(StringUtils::format("%d", SaveInput).c_str());
	_tfSetProRatio->setVisible(true);

    //temp = InputNum1;
    auto pSendMsg = static_cast<Button*>(pGNS_PromoterPro->getChildByName("PromoterBtn"));
    pSendMsg->setTag(1);
    pSendMsg->addTouchEventListener(CC_CALLBACK_2(GNS_PromoterPro::BtnCallBack, this));
    
    
    /*
    pInputNun= GNS_CursorTextField::textFieldWithPlaceHolder("0",GameFontName, 28);
    pInputNun->setColor(Color3B::BLACK);
    pInputNun->setAnchorPoint(Vec2(0, 0.5f));
    pInputNun->setPosition(Vec2(winsize.width*0.43, InputNum->getPositionY()));
    pInputNun->setHorizontalAlignment(TextHAlignment::LEFT);
    
    //pInputNun->AddCursor(pGNS_PromoterPro);
    pInputNun->setLimitNum(10);*/
    
}
void GNS_PromoterPro::BtnCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type){
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int BtnTag = pButton->getTag();
        switch (BtnTag) {
            case 10:
            {
                if (GNS_PlayerMgr::getInstance()->_NodeChildVec.size()!=0) {
                    GNS_PlayerMgr::getInstance()->_NodeChildVec.clear();
                    this->removeAllChildrenWithCleanup(true);
                    this->getParent()-> removeChild(this);
                }
                //auto SystemNode=GNS_PlayerMgr::getInstance()->_NodeHallVec.at(0);
                //SystemNode->setVisible(true);

				GNS_NodeMgr::getInstance()->setMenuVisible("SystemMgr", true);
                break;
            }
            case 1:
            {
                //if (pInputNun->getString().length()==0) {
                //    auto pPopWnd = PopWndLayer::createScene("Popwnd.png", "PageClose.png");
                //    pPopWnd->setClickWndOutsideClose(true);
                //    pPopWnd->SetTitle("提示!");
                //    pPopWnd->SetContentText("充值比例为空", Color3B::BLACK);
                //}else{
                //    CMD_CS_SetPromoterRechargeRatio pSetPromoterRecRatio;
                //    pSetPromoterRecRatio._goldPerRmb=atoi(pInputNun->getString().c_str());
                //    pSetPromoterRecRatio._agentCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
                //    //_GameSock->sendData(MAIN_MSG_LOGIN, SUB_CS_SET_PROMOTER_RECHANGE_RATIO, &pSetPromoterRecRatio, sizeof(pSetPromoterRecRatio));
                //    GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_SET_PROMOTER_RECHANGE_RATIO, (char*)&pSetPromoterRecRatio, sizeof(pSetPromoterRecRatio));
                //    SaveInput=atoi(pInputNun->getString().c_str());

                //     if (GNS_PlayerMgr::getInstance()->getAgRation() && (GNS_PlayerMgr::getInstance()->getAgRation())->size()>0)
                //         GNS_PlayerMgr::getInstance()->getAgRation()->at(0)._lowlineRechargeRatio = SaveInput;
                //}
				//string protmp = tfSetProRatio->getString();
				string protmp = _tfSetProRatio->getText();
				if (protmp == "0" || !CCGnsOften::isNumber(protmp)) {
					auto pPopWnd = PopWndLayer::createScene("Popwnd.png", "PageClose.png");
					pPopWnd->setClickWndOutsideClose(true);
					pPopWnd->SetTitleWithUTF("提示!");
					pPopWnd->SetContentTextWithUTF("充值比例有误！", Color3B::BLACK);
				}
				else{
					CMD_CS_SetPromoterRechargeRatio pSetPromoterRecRatio;
					pSetPromoterRecRatio._goldPerRmb = atoi(_tfSetProRatio->getText());
					pSetPromoterRecRatio._agentCode = (int)GNS_PlayerMgr::getInstance()->getMyUseridx();
					//_GameSock->sendData(MAIN_MSG_LOGIN, SUB_CS_SET_PROMOTER_RECHANGE_RATIO, &pSetPromoterRecRatio, sizeof(pSetPromoterRecRatio));
					GnsClientFrame::getInstance()->sendMsg(0, MAIN_MSG_LOGIN, SUB_CS_SET_PROMOTER_RECHANGE_RATIO, (char*)&pSetPromoterRecRatio, sizeof(pSetPromoterRecRatio));
					SaveInput = atoi(_tfSetProRatio->getText());

					vector<CMD_SC_GetAgentRechargeRatioResult> *pRecharge = GNS_PlayerMgr::getInstance()->getAgRation();
					if (!pRecharge->empty())
					{
						vector<CMD_SC_GetAgentRechargeRatioResult>::iterator pRechargeIter = pRecharge->begin();
						for (; pRechargeIter != pRecharge->end(); ++pRechargeIter)
						{
							if (pRechargeIter->_lowlineCode == 0)
								pRechargeIter->_lowlineRechargeRatio = SaveInput;
						}
					}
				}
            }
            default:
                break;
        }
    }
}
int GNS_PromoterPro::saveThePutNumber()
{
    return SaveInput;
}

void GNS_PromoterPro::setRechargeRatio(const int rechargeRatio)
{
	SaveInput = rechargeRatio;
	_tfSetProRatio->setText(StringUtils::format("%d", SaveInput).c_str());
}

void GNS_PromoterPro::SetInputStr(std::string InputStr)
{
    //pInputNun = GNS_SceneMgr::getInstance()->MycreateEditBox(InputStr,Vec2(winsize.width*0.43, temp->getPositionY()), 24, Color3B::BLACK, 10, false);
    //pGNS_PromoterPro->addChild(pInputNun,12);
}
void GNS_PromoterPro::ResProRation()
{
    GNS_PlayerMgr::getInstance()->getAgRation()->clear();
    CMD_CS_GetAgentRechargeRatio pGetRecRation;
    pGetRecRation._agentCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
    GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_LOWLINE_RECHARGE_RATIO, (char*)&pGetRecRation, sizeof(pGetRecRation));
}